package com.winchain.chainbill.bill.biz.receive.rx;

import com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException;
import com.shell.framework.rx.OnErrorNotImplementedSubscriber;
import com.winchain.chainbill.bill.base.po.CbPye;
import com.winchain.chainbill.bill.base.po.CbRcv;
import com.winchain.chainbill.bill.base.service.CbPyeService;
import com.winchain.chainbill.bill.biz.Bill;
import com.winchain.chainbill.bill.biz.BillEvent;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class BillPayeeSubscriber extends OnErrorNotImplementedSubscriber<BillEvent<CbRcv>> {
	private final Log logger = LogFactory.getLog(getClass());
	@Autowired
	private CbPyeService cbPayeeService;

	public void onApplicationEvent(BillEvent<CbRcv> t) {
		CbRcv cbRcv = (CbRcv) t.getBill().getSource();

		CbPye payee = new CbPye();
		payee.setBuyerId(cbRcv.getBuyerId());
		payee.setSellerId(cbRcv.getSellerId());
		if (this.cbPayeeService.selectOne(payee) == null) {
			try {
				this.cbPayeeService.save(payee);
			} catch (Exception e) {
				if ((e instanceof MySQLIntegrityConstraintViolationException)) {
					this.logger.info("CbPye Save Failed:MySQLIntegrityConstraintViolationException", e);
				} else {
					this.logger.error("CbPye Save Failed.", e);
				}
			}
		}
	}
}
